<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px" v-hasPermi="['zhhq:canteenorder:query']">
			<el-form-item label="时间">
				<el-date-picker v-model="dateRange" style="width: 350px;" value-format="yyyy-MM-dd HH:mm:ss"
					type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="餐厅" prop="restauranId">
				<el-select v-model="queryParams.restauranId" placeholder="请选择餐厅" clearable>
					<el-option v-for="item in canteenList" :key="item.restauranId" :label="item.restauranName"
						:value="item.restauranId" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['zhhq:canteenorder:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['zhhq:canteenorder:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['zhhq:canteenorder:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['zhhq:canteenorder:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="canteenorderList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="订单编号" align="center" prop="orderId" />
			<el-table-column label="用餐时间" align="center" prop="mealTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.mealTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="餐厅" align="center" prop="zhhqCantee.restauranName" />
			<el-table-column label="审核状态" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.zhhq_sh" :value="scope.row.status" />
				</template>
			</el-table-column>
<!--			<el-table-column label="评价" align="center" prop="remark" />-->
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['zhhq:canteenorder:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['zhhq:canteenorder:remove']">删除</el-button>
					<el-button size="mini" type="text" icon="el-icon-success" @click="handleCheck(scope.row)"
						v-hasPermi="['zhhq:canteenorder:edit']">审核</el-button>
<!--					<el-button size="mini" type="text" v-show="scope.row.status=='0'?true:false"-->
<!--						icon="el-icon-star-on" @click="handleEvaluate(scope.row)"-->
<!--						v-hasPermi="['zhhq:canteenorder:edit']">评价</el-button>-->
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改餐厅订单对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="订单编号" prop="orderId">
					<el-input v-model="form.orderId" placeholder="请输入订单编号" :disabled="isUpdate" />
				</el-form-item>
				<el-form-item label="用餐时间" prop="mealTime">
					<el-date-picker clearable v-model="form.mealTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
						placeholder="请选择用餐时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="餐厅" prop="restauranId">
					<el-select v-model="form.restauranId" placeholder="请选择餐厅" clearable>
						<el-option v-for="item in canteenList" :key="item.restauranId" :label="item.restauranName"
							:value="item.restauranId" />
					</el-select>
				</el-form-item>
				<el-form-item label="审核状态" prop="status" v-if="!isUpdate">
					<el-radio-group v-model="form.status">
						<el-radio v-for="dict in dict.type.zhhq_sh" :key="dict.value" :label="dict.value">
							{{dict.label}}
						</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 审核订单对话框 -->
		<el-dialog :title="title" :visible.sync="opencheck" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="审核状态" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio v-for="dict in dict.type.zhhq_sh" :key="dict.value" :label="dict.value">
							{{dict.label}}
						</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitCheckForm">确 定</el-button>
				<el-button @click="cancelCheck">取 消</el-button>
			</div>
		</el-dialog>


		<!-- 评价订单对话框 -->
		<el-dialog :title="title" :visible.sync="openEvaluate" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="评价" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitEvaluateForm">确 定</el-button>
				<el-button @click="cancelEvaluate">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listCanteenorder,
		getCanteenorder,
		delCanteenorder,
		addCanteenorder,
		updateCanteenorder
	} from "@/api/zhhq/canteenorder";
	import {
		listCantee
	} from "@/api/zhhq/cantee"
import { addOrder } from "@/api/zhhq/order"
	export default {
		name: "Canteenorder",
		dicts: ['sys_normal_disable', 'zhhq_sh'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				//当前操作是修改吗？
				isUpdate: false,
				// 餐厅订单表格数据
				canteenorderList: [],
				//所有餐厅列表
				canteenList: [],
				// 日期范围
				dateRange: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 是否显示审核弹出层
				opencheck: false,
				// 是否显示评价弹出层
				openEvaluate: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					mealTime: null,
					restauranId: null,
					status: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					orderId: [{
						required: true,
						message: "订单编号不能为空",
						trigger: "blur"
					}],
					mealTime: [{
						required: true,
						message: "用餐时间不能为空",
						trigger: "blur"
					}],
					status: [{
						required: true,
						message: "状态不能为空",
						trigger: "blur"
					}],
					restauranId: [{
						required: true,
						message: "餐厅不能为空",
						trigger: "blur"
					}],
				}
			};
		},
		created() {
			this.getList();
			this.getCanteenList();
		},
		methods: {
			/** 查询餐厅订单列表 */
			getList() {
				this.loading = true;
        if (this.$store.state.user.name !== 'admin') {
          listCanteenorder({
            createBy: this.$store.state.user.name
          }).then(response => {
            this.canteenorderList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        } else {
          listCanteenorder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.canteenorderList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
			},
			getCanteenList() { //查询所有餐厅列表
				listCantee().then(
					response => {
						this.canteenList = response.rows;
					}
				);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 审核取消按钮
			cancelCheck() {
				this.opencheck = false;
				this.reset();
			},// 审核取消按钮
			cancelEvaluate() {
				this.openEvaluate = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					orderId: null,
					mealTime: null,
					restauranId: null,
					status: "0",
					createBy: this.$store.state.user.name,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: null
				};
				this.isUpdate = false;
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.dateRange = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.orderId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加餐厅订单";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const orderId = row.orderId || this.ids
				getCanteenorder(orderId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改餐厅订单";
					this.isUpdate = true;
				});
			},
			/** 审核按钮操作 */
			handleCheck(row) {
				this.reset();
				const orderId = row.orderId || this.ids
				getCanteenorder(orderId).then(response => {
					this.form = response.data;
					this.opencheck = true;
					this.title = "审核餐厅订单";
				});
			},
			/** 评价按钮操作 */
			handleEvaluate(row) {
				this.reset();
				const orderId = row.orderId || this.ids
				getCanteenorder(orderId).then(response => {
					this.form = response.data;
					this.openEvaluate = true;
					this.title = "评价餐厅订单";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.isUpdate) {
							updateCanteenorder(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
              addOrder({
                orderId: this.form.orderId,
                ocateName: "餐厅订单",
                status: "0",
                createBy: this.form.createBy
              }).then(response => {
                addCanteenorder(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              })
						}
					}
				});
			},
			/** 审核提交按钮 */
			submitCheckForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						updateCanteenorder(this.form).then(response => {
							this.opencheck = false;
							this.getList();
						});

					}
				});
			},
			/** 评价提交按钮 */
			submitEvaluateForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						updateCanteenorder(this.form).then(response => {
							this.openEvaluate = false;
							this.getList();
						});

					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const orderIds = row.orderId || this.ids;
				this.$modal.confirm('确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
					return delCanteenorder(orderIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('zhhq/canteenorder/export', {
					...this.queryParams
				}, `canteenorder_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
